home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / www / cern / dev / scott.Z / scott / WWW / NextStep / Implementation / old / HTTP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-11  |  2.5 KB  |  104 lines

  1. /*    HyperText Tranfer Protocol    - Client implementation        HTTP.c
  2. **    ==========================
  3. */
  4.  
  5. /*    Module parameters:
  6. **    -----------------
  7. **
  8. **  These may be undefined and redefined by syspec.h
  9. */
  10. #include "HTParse.h"
  11. #include "HTUtils.h"
  12. #include "tcp.h"
  13. #include "HTTCP.h"
  14.  
  15.  
  16. /*        Open Socket for reading from HTTP Server    HTTP_get()
  17. **        ========================================
  18. **
  19. **    Given a hypertext address, this routine opens a socket to the server.
  20. **
  21. ** On entry,
  22. **    arg    is the hypertext reference of the article to be loaded.
  23. ** On exit,
  24. **    returns    >=0    If no error, a good socket number
  25. **        <0    Error.
  26. **
  27. **    The socket must be closed by the caller after the document has been
  28. **    read.
  29. **
  30. */
  31. #ifdef __STDC__
  32. int HTTP_Get(const char * arg)
  33. #else
  34. int HTTP_Get(arg)
  35.     char * arg;
  36. #endif
  37. {
  38.     int s;                /* Socket number for returned data */
  39.     char command[257];            /* The whole command */
  40.     int status;                /* tcp return */
  41.  
  42.     struct sockaddr_in soc_address;    /* Binary network address */
  43.     struct sockaddr_in* sin = &soc_address;
  44.  
  45.     if (!arg) return -3;            /* Bad if no name sepcified    */
  46.     if (!*arg) return -2;            /* Bad if name had zero length    */
  47.  
  48. /*  Set up defaults:
  49. */
  50.     sin->sin_family = AF_INET;                /* Family, host order  */
  51.     sin->sin_port = htons(TCP_PORT);            /* Default: new port,    */
  52.  
  53.     if (TRACE) printf("HTTPAccess: Looking for %s\n", arg);
  54.  
  55. /* Get node name and optional port number:
  56. */
  57.     {
  58.     char *p1 = HTParse(arg, "", PARSE_HOST);
  59.     HTParseInet(sin, p1);
  60.         free(p1);
  61.     }
  62.     
  63. /* We will ask that node for the document, omitting the host name & anchor.
  64. */        
  65.     strcpy(command, "GET ");
  66.     {
  67.     char * p1 = HTParse(arg, "", PARSE_PATH|PARSE_PUNCTUATION);
  68.     strcat(command, p1);
  69.     free(p1);
  70.     }
  71.     strcat(command, "\n");
  72.         
  73.    
  74. /*    Now, let's get a socket set up from the server for the sgml data:
  75. */      
  76.     s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  77.     status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
  78.     if (status<0){
  79.     if (TRACE) printf("HTTPAccess: Unable to connect to remote host for `%s'.\n",
  80.         arg);
  81.     return HTInetStatus("connect");
  82.     }
  83.     
  84.     if (TRACE) printf("HTTP connected, socket %d\n", s);
  85.     if (TRACE) printf("HTTP writting command `%s' to socket %d\n", command, s);
  86.     
  87. #ifdef NOT_ASCII
  88.     {
  89.         char * p;
  90.     for(p = command; *p; p++) {
  91.         *p = TOASCII(*p);
  92.     }
  93.     }
  94. #endif
  95.  
  96.     status = NETWRITE(s, command, strlen(command));
  97.     if (status<0){
  98.     if (TRACE) printf("HTTPAccess: Unable to send command.\n");
  99.         return HTInetStatus("send");
  100.     }
  101.  
  102.     return s;            /* Good return */
  103. }
  104.